﻿using FluentValidation;
using Microsoft.Extensions.Logging;
using System.Net.Http;

namespace Sgr.Exceptions
{
    /// <summary>
    /// 验证异常处理器
    /// </summary>
    public class ValidationExceptionHandler : IExceptionHandler
    {
        public bool TryTranslate(Exception exception, ref string message)
        {
            if (exception is ValidationException validationEx)
            {
                // 记录详细的验证错误信息
                var errorMessages = validationEx.Errors
                    .Select(e => $"属性 '{e.PropertyName}': {e.ErrorMessage}")
                    .ToList();

                message = $"验证错误: {validationEx.Errors.Count()} 个错误, 详情: {string.Join("; ", errorMessages)}";

                return true;
            }

            return false;
        }
    }
}